<?php
add_action('widgets_init', 'bhutanese_refugees_load_recent_category_post_widgets');

/**
 * Register our widget.
 * 'Example_Widget' is the widget class used below.
 *
 * @since 0.1
 */
function bhutanese_refugees_load_recent_category_post_widgets() {
    register_widget('Bhutanese_Refugees_Recent_Category_Post_Widget');
}

/**
 * Example Widget class.
 * This class handles everything that needs to be handled with the widget:
 * the settings, form, display, and update.  Nice!
 *
 * @since 0.1
 */
class Bhutanese_Refugees_Recent_Category_Post_Widget extends WP_Widget {

    /**
     * Widget setup.
     */
    function Bhutanese_Refugees_Recent_Category_Post_Widget() {
        /* Widget settings. */
        $widget_ops = array('classname' => 'bhutanese_refugees', 'description' => __('Widgets for displaying recent post of particular category', 'bhutanese_refugees'));

        /* Widget control settings. */
        $control_ops = array('width' => 300, 'height' => 350, 'id_base' => 'bhutanese-refugees-recent-category-post-widgets');

        /* Create the widget. */
        $this->WP_Widget('bhutanese-refugees-recent-category-post-widgets', __('Bhutanese rufugees recent category post widget', 'bhutanese_refugees'), $widget_ops, $control_ops);
    }

    /**
     * How to display the widget on the screen.
     */
    function widget($args, $instance) {
        extract($args);
        /* Our variables from the widget settings. */
        $title = apply_filters('widget_title', empty($instance['title']) ? __('') : $instance['title']);
        $category_id = $instance['category_id'];
        $no_of_post = is_numeric($instance['no_of_post'])?$instance['no_of_post']:5;
        $excerpt_length = $instance['excerpt_length'];

        echo $before_widget;
        echo $before_title;
        echo $title;
        echo $after_title;
        if(is_numeric($category_id)){
            $args = array(
              'cat'=>$category_id,
              'posts_per_page'=>$no_of_post
            );
        }else{
            $args = array(
              'posts_per_page'=>$no_of_post
            );
        }
        query_posts($args);
        if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<div class="sidebar-list-post">
                                    <a href="<?php the_permalink(); ?>"><b><?php the_title(); ?></b></a><br />
                                    <?php if($excerpt_length>0){?>
                                    <span>
                                        <?php echo excerpt($excerpt_length);?>
                                    </span>
                                    <?php }?>
				</div>
<?php endwhile; // end of the loop.
        wp_reset_query();
        /* After widget (defined by themes). */
        echo $after_widget;
    }

    /**
     * Update the widget settings.
     */
    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        /* Strip tags for post_title,post_no_show,categories_title,comment_title and comment_no_show to remove HTML (important for text inputs). */
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['category_id'] = strip_tags($new_instance['category_id']);
        $instance['no_of_post'] = strip_tags($new_instance['no_of_post']);
        $instance['excerpt_length'] = strip_tags($new_instance['excerpt_length']);
        return $instance;
    }

    /**
     * Displays the widget settings controls on the widget panel.
     * Make use of the get_field_id() and get_field_name() function
     * when creating your form elements. This handles the confusing stuff.
     */
    function form($instance) {
//                wp_deregister_script( 'jquery' );
//                wp_register_script( 'jquery', 'http://code.jquery.com/jquery-latest.pack.js', false, '' );
//                add_action('wp_print_scripts',  wp_enqueue_script('jquery'));
        /* Set up some default widget settings. */
        $defaults = array();
        $instance = wp_parse_args((array) $instance, $defaults);
        ?>
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'bhutanese_refugees'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" type="text"/>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('category_id'); ?>">Recent Post of Category:</label>
            <?php 
            $args = array(
                'show_option_all'    => 'All category',
                'show_option_none'   => '',
                'orderby'            => 'ID', 
                'order'              => 'ASC',
                'show_count'         => 1,
                'hide_empty'         => 1, 
                'child_of'           => 0,
                'exclude'            => '',
                'echo'               => 1,
                'selected'           => $instance['category_id'],
                'hierarchical'       => 1, 
                'name'               => $this->get_field_name('category_id'),
                'id'                 => $this->get_field_id('category_id'),
                'class'              => 'postform',
                'depth'              => 0,
                'tab_index'          => 0,
                'taxonomy'           => 'category',
                'hide_if_empty'      => false
            );
            wp_dropdown_categories( $args ); ?> 
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('no_of_post'); ?>">Number of Post:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('no_of_post'); ?>" name="<?php echo $this->get_field_name('no_of_post'); ?>" value="<?php echo $instance['no_of_post']; ?>" type="text"/>
            <span>Note: default 5.</span>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('excerpt_length'); ?>">Excerpt Length:</label>
            <input class="widefat" id="<?php echo $this->get_field_id('excerpt_length'); ?>" name="<?php echo $this->get_field_name('excerpt_length'); ?>" value="<?php echo $instance['excerpt_length']; ?>" type="text"/>
            <span>Note: if '0' is applied then excerpt is turn off.</span>
        </p>
        <?php
    }

}
?>